#include <stdio.h>
#include <stdlib.h>
#include "../include/my_list.h"

my_list* my_list_init(){
    my_list* list = malloc(sizeof(my_list));
    list->next = NULL;
    list->len=0;
    return list;
}

void my_list_add(my_list* list, int fd){
    if(my_list_contains(list, fd))
        return;
    //头插法
    my_node* node = malloc(sizeof(my_node));
    node->fd = fd;
    node->next = list->next;
    list->next = node;
    list->len++;
}

int my_list_contains(my_list* list, int fd){
    my_node* node= list->next;
    while(node){
        if(node->fd == fd)
            return 1;
        node = node->next;
    }
    return 0;
}

void my_list_remove(my_list* list, int fd){
    my_node* node= list->next;
    my_node* pre= list;
    while(node){
        if(node->fd != fd){//要删除的节点
            pre = node;
            node = node->next;
            continue;
        }
        pre->next = node->next;
        list->len--;
        free(node);
        node = NULL;
        return;
    }
}